var map;
var markers = [];
var infowindow = new google.maps.InfoWindow();
var current_latLng;
var current_address;
function initialize() {

  var map_lat = 1;
  var map_lng=1;
  var image_latLgns_array = map_object.image_latLgns.split(';');
 for(var i=0;i<image_latLgns_array.length-1;i++)
  {
      var latLng_array=image_latLgns_array[i].split(',');
      map_lat=latLng_array[0];
      map_lng=latLng_array[1];
      var latLng_set=new google.maps.LatLng(latLng_array[0], latLng_array[1]);
      add_Exist_Marker(latLng_set);
  }

  var mapOptions = {
    center: new google.maps.LatLng(map_lat,map_lng),
    zoom: 13
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
    mapOptions);

  // Create the search box and link it to the UI element.
  var input = /** @type {HTMLInputElement} */(
      document.getElementById('pac-input'));
  var types = document.getElementById('save-location');
  map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
  map.controls[google.maps.ControlPosition.TOP_LEFT].push(types); 

  var searchBox = new google.maps.places.SearchBox(
    /** @type {HTMLInputElement} */(input));

  // Listen for the event fired when the user selects an item from the
  // pick list. Retrieve the matching places for that item.
  if(map_object.viewmode!='yes')
  google.maps.event.addListener(searchBox, 'places_changed', function() {
    var places = searchBox.getPlaces();
    if (places.length == 0) {
      return;
    }
    for (var i = 0, marker; marker = markers[i]; i++) {
      marker.setMap(null);
    }
    // For each place, get the icon, place name, and location.
    markers = [];
    var bounds = new google.maps.LatLngBounds();
    for (var i = 0, place; place = places[i]; i++) {
      bounds.extend(place.geometry.location);
    }
    map.fitBounds(bounds);
  });

  // Bias the SearchBox results towards places that are within the bounds of the
  // current map's viewport.
  if(map_object.viewmode!='yes')
  google.maps.event.addListener(map, 'bounds_changed', function() {
    var bounds = map.getBounds();
    searchBox.setBounds(bounds);
  });
  if(map_object.viewmode!='yes')
 google.maps.event.addListener(map, 'click', function(event) {
    deleteMarkers();
    addMarker(event.latLng);
  });
   //them cac marker cho cac vi tri anh dc chon

  
}
function addMarker(location) {
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({'latLng': location}, function(results, status) {
  var marker = new google.maps.Marker({
    position: location,
    map: map
  });
  current_address=results[1].formatted_address;
  current_latLng=location.lat()+','+location.lng();
  infowindow.setContent(results[1].formatted_address);
  infowindow.open(map, marker);
  markers.push(marker);
  });
}
function add_Exist_Marker(location) {
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({'latLng': location}, function() {
  var marker = new google.maps.Marker({
    position: location,
    map: map
  });
  markers.push(marker);
  });
}

function clearMarkers() {
 for (var i = 0; i < markers.length; i++) {
    markers[i].setMap(null);
  }
}

function deleteMarkers() {
  clearMarkers();
  markers = [];
}
google.maps.event.addDomListener(window, 'load', initialize);

jQuery(document).ready(function()
  {
    jQuery("#save-location").click(function()
      {
        jQuery.ajax({
          type:'POST',
          url:map_object.ajaxurl,
          data:
          {
            action:'save_location',
            image_ids:map_object.image_ids,
            address:current_address,
            latLng:current_latLng

          },
          success:function(data)
          {
            window.close();
            //jQuery('#page-content').html(data);
          }
        });
      });
  });